<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Models\ClassCourse;
use App\Models\Teacher;
use App\Models\Student;
use App\Models\Semester;
use App\Http\Controllers\Controller;
use App\Models\Veidoo;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\Details;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use App\admin\Extensions\InsertTCQE;
use App\Admin\Extensions\ScoreHandle;
use App\Models\Course;
use App\Models\VeidooScore;
use Illuminate\Http\Request;


class ListCQEController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {

        $content->header('综合素质评价');
            $content->description(' ');
            $content->body($this->grid());
        $content->body('<div>*只有教师、小组、自我评价完才会显示数据</div>');
        return $content;
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('综合素质评价')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {

        $content->header('综合素质评价');
        $content->description(' ');
        $content->body($this->form()->edit($id));


        return $content;
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('综合素质评价')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $semester = Semester::where('is_semester', 1)->first();
        $semester_id = $semester->id;

        $stutdent_arr = VeidooScore::select(['student_id'])->where('semester_id', $semester_id)->groupBy('student_id')->get();

        $result = CommonFcController::StudentVeidooScore($stutdent_arr, $semester_id);

        $final_student_arr = $result['final_student_arr'] ;
        $student_id_arr = $result['student_id_arr'] ;
        $weight_arr = $result['weight_arr'] ;
        $grid = new Grid(new Student);
        $grid->model()->whereIn('id', $student_id_arr);
        $grid->id('ID');
        $grid->student_id('学号');
        $grid->name('姓名');
        $grid->column('class.class_name','班级');
        foreach ($weight_arr as $key => $weight) {
            $grid->column($weight['name'])->display(function () use ($final_student_arr,$key) {
                return $final_student_arr[$this->id]['weight'][$key];
            });
        }
        $grid->column('final_score','总分')->display(function () use ($final_student_arr){
            return $final_student_arr[$this->id]['final_score'];
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->like('student_id', '学号');
            $filter->like('name', '姓名');

        });
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
        $grid->paginate(20);
        $grid->disableCreateButton();
//        $grid->disableFilter();
        $grid->disableActions();
        $grid->disableRowSelector();
        $grid->disableExport();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {

    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Grade);

        return $form;
    }
}
